package com.lik.javadeep.basic.sort;

public class SelectionSortRepeat {
    public static void main(String[] args) {
        int[] ints = {9, 3, 5, 2, 9, 6, 18};
        selectAndSort(ints);

        for (int i : ints) {
            System.out.println(i);
        }

    }

    private static void selectAndSort(int[] ints) {
        int sortedBeginIndex;
        int unsortedBeginIndex;
        int maxIndex = ints.length - 1;
        for (int i = 0; i < ints.length - 1; i++) {

            // 每经过一轮，已有序的区域大小必定+1
            sortedBeginIndex = ints.length - (i + 1);
            // 寻找最大的,在未有序的区域，从后往前
            maxIndex = sortedBeginIndex - 1;
            for (int j = 0; j <= sortedBeginIndex; j++) {

                if (ints[sortedBeginIndex - j] > ints[maxIndex]) {
                    maxIndex = sortedBeginIndex - j;
                }
            }

            System.out.println("本轮找到的max：" + ints[maxIndex]);
            swap(ints, sortedBeginIndex, maxIndex);

        }
    }


    private static void swap(int[] array, int aIndex, int bIndex) {
        int temp = array[aIndex];
        array[aIndex] = array[bIndex];
        array[bIndex] = temp;
    }
}
